const user = require('../controllers/user')
const message = require('../controllers/message')
const group = require('../controllers/group')
const { formatBody } = require('../tools')

const initSocketListener = io => {
  io.sockets.on('connection', socket => {
    console.log('************connection************')
    // 登录
    socket.on('login', (info, cb) => {
      user.login(info, socket, cb)
    })
    // 自动登录
    socket.on('autoLogin', (info, cb) => {
      user.autoLogin(info, socket, cb)
    })
    // 注册
    socket.on('signUp', (info, cb) => {
      user.signUp(info, socket, cb)
    })
    // 下线
    socket.on('disconnect', (info) => {
      user.disconnect(info, socket, io)
    })
    // 随机匹配
    socket.on('updateMathState', (info, cb) => {
      user.updateMathState(info, socket, cb, io)
    })
    // 获取用户信息
    socket.on('getUserInfo', (info, cb) => {
      user.getUserInfo(info, socket, cb)
    })
    // 获取私聊信息
    socket.on('getPrivateMsg', (info, cb) => {
      message.getPrivateMsg(info, socket, cb)
    })
    // 创建群组
    socket.on('createGroup', (info, cb) => {
      group.createGroup(info, socket, cb)
    })
    // 加入群组
    socket.on('joinGroup', (info, cb) => {
      group.joinGroup(info, socket, cb)
    })
    // 搜索群组
    socket.on('searchGroup', (info, cb) => {
      group.searchGroup(info, socket, cb)
    })
    // 获取群组列表
    socket.on('getGroupList', (info, cb) => {
      group.getGroupList(info, socket, cb)
    })
    // 获取群信息
    socket.on('getGroupInfo', (info, cb) => {
      group.getGroupInfo(info, socket, cb)
    })
    // 更新群组信息
    socket.on('updateGroupInfo', (info, cb) => {
      group.updateGroupInfo(info, socket, cb)
    })
    // 获取群组消息记录
    socket.on('getGroupMsg', (info, cb) => {
      message.getGroupMsg(info, socket, cb)
    })
    // 获取群组成员
    socket.on('getGroupMembers', (info, cb) => {
      group.getGroupMembers(info, socket, cb)
    })
    // 发送新消息
    socket.on('new message', (info, cb) => {
      message.newMessage(info, socket, cb, io)
    })

  })
}
module.exports = initSocketListener
